package cn.cqautotest.cqviehelper.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ArrayRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import cn.cqautotest.cqviehelper.config.Constants;

import com.gyf.immersionbar.ImmersionBar;

import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment<A extends BaseActivity> extends Fragment {

    protected final String TAG = getClass().getSimpleName();
    /**
     * Activity 对象
     */
    private A mActivity;
    /**
     * 根布局
     */
    protected View mRootView;
    private Unbinder mBind;
    protected boolean isFirstLoad = true;
    protected Handler mHandler;
    protected Context mContext;
    protected SharedPreferences mSpUserInfoSetting;

    @SuppressWarnings("unchecked")
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 获得全局的 Activity
        mActivity = (A) requireActivity();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }

    /**
     * 获取绑定的 Activity，防止出现 getActivity 为空
     */
    @NonNull
    public A getAttachActivity() {
        return mActivity;
    }

    /**
     * 简化Activity的跳转
     *
     * @param activity 当前Activity
     */
    public void startActivity(Class<?> activity) {
        startActivity(new Intent(mActivity, activity));
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(getContext()).inflate(getLayoutResId(), container, false);
    }

    protected abstract int getLayoutResId();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mBind = ButterKnife.bind(this, view);
        mHandler = new Handler();
        mRootView = view;
        mContext = view.getContext();
        //存储用户信息的相关设置
        mSpUserInfoSetting = mContext.getSharedPreferences(Constants.USER_INFO_SETTING, Context.MODE_PRIVATE);
        int barHeight = ImmersionBar.getStatusBarHeight(this);
        getLayoutContainer().setPadding(0, barHeight, 0, 0);
        initPresenter();
        initView(view);
        initData();
        initEvent();
    }

    protected View getLayoutContainer() {
        return getView();
    }

    /**
     * 根据资源 id 获取一个 View 对象
     */
    public <V extends View> V findViewById(@IdRes int id) {
        return mRootView.findViewById(id);
    }

    protected void initPresenter() {

    }

    protected void initEvent() {

    }

    /**
     * 界面加载完毕的时候调用，只会被调用一次
     */
    protected void initData() {

    }

    /**
     * 用于初始化View
     *
     * @param rootView 根布局
     */
    protected void initView(View rootView) {

    }

    /**
     * Return the string array associated with a particular resource ID.
     *
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     * @return The string array associated with the resource.
     * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
     */
    protected String[] getStringArray(@ArrayRes int id) {
        return getResources().getStringArray(id);
    }

    @Override
    public void onResume() {
        super.onResume();
        uiReShowing();
        if (isFirstLoad) {
            lazyLoadData();
            isFirstLoad = false;
        }
    }

    /**
     * 每次显示的时候都会被调用
     */
    protected void uiReShowing() {

    }

    /**
     * 只有用户可见的时候才会被调用
     */
    protected void lazyLoadData() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mBind != null) mBind.unbind();
        release();
        if (mHandler != null) mHandler = null;
    }

    /**
     * 当Fragment被销毁时调用
     */
    protected void release() {

    }
}
